<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />

          <el-table-column label="操作" align="center" fixed="right" width="120">
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['device:checkPlan:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <el-button
                v-hasPermi="['device:checkPlan:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom" :class="userName=='嵇茂山'||userName=='王杰'?'spacebetween':''">
      <!--    <div class="main_bottom">-->

      <a v-if="userName=='嵇茂山'||userName=='王杰'" class="independent_button" @click="batchEdit">
        <svg-icon icon-class="批量操作" />
        批量编辑
      </a>
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { list, remove, getInfo } from '@/api/equipmentManage/equipmentCheck/checkPlan'
import { getTemplate } from '@/api/equipmentManage/equipmentCheck/checkTemplate'
import { getEquipmentList1 } from '@/api/equipmentManage/baseInfo/equipmentInfo'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
import ShowDetailOld from '@/components/JLINK2/common/ShowDetailOld' // 查看页面
import BatchEdit from './batchEdit.vue' // 查看页面
import { mapGetters } from 'vuex'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '点检计划',
      pageData: {
        companyName: undefined,
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'code',
        isAsc: 'desc'
      },
      total: 1, // 数据一共有多少条
      mainId: 'id',
      tableHeight: '0',
      tableData: [],
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '计划状态', minWidth: '100', canOrder: false },
        { prop: 'code', label: '点检计划单号', sortable: 'custom' },
        { prop: 'bookName', label: '点检设备', minWidth: '200', canOrder: false },
        { prop: 'templateName', label: '点检模版', minWidth: '200', canOrder: false },
        { prop: 'startTime', label: '计划开始时间', minWidth: '140', sortable: 'custom' },
        { prop: 'endTime', label: '计划结束时间', minWidth: '140', sortable: 'custom' },
        { prop: 'lastTime', label: '最后点检时间', minWidth: '140', sortable: 'custom' },
        { prop: 'nextTime', label: '下次点检时间', minWidth: '140', sortable: 'custom' },
        { prop: 'taskModeName', label: '任务启动方式', canOrder: false },
        { prop: 'period', label: '点检周期', canOrder: false },
        { prop: 'periodUnit', label: '周期单位', canOrder: false },
        { prop: 'remind', label: '任务提醒时间(分)', canOrder: false },
        { prop: 'timeOutNumber', label: '合理逾期时间(分)', canOrder: false },
        // { prop: 'baseMaintenanceName', label: '维保班组' },
        { prop: 'checkPersonName', label: '点检人员', canOrder: false },
        { prop: 'shiftLabel', label: '班次', canOrder: false },
        { prop: 'remark', label: '备注', canOrder: false, minWidth: '200' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        // { prop: 'code', label: '点检计划单号', type: 'input' },
        { prop: 'bookName', label: '点检设备', type: 'select', optionsName: 'equipmentList' },
        { prop: 'templateName', label: '点检模版', type: 'select', optionsName: 'templateList' },
        { prop: 'status', label: '计划状态', type: 'select', optionsName: 'statusOptions' },
        { prop: 'startTime', label: '计划开始时间', type: 'time' },
        { prop: 'endTime', label: '计划结束时间', type: 'time' },
        { prop: 'lastTime', label: '最后点检时间', type: 'time' },
        { prop: 'nextTime', label: '下次点检时间', type: 'time' },
        { prop: 'taskMode', label: '任务启动方式', type: 'select', optionsName: 'taskModeOptions' },
        // { prop: 'period', label: '点检周期', type: 'input' },
        { prop: 'periodUnit', label: '周期单位', type: 'select', optionsName: 'periodUnitOptions' }
        // { prop: 'checkPersonName', label: '点检人员', type: 'input' },
        // { prop: 'shiftLabel', label: '班次', type: 'input' }
      ],

      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'device:checkPlan:add' },
        { label: '批量删除', perm: 'device:checkPlan:delete' },
        { label: '页面配置', perm: '' }
      ],
      templateList: [],
      equipmentList: [],
      statusOptions: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: '0', label: '暂停' },
        { value: '1', label: '持续执行' }
      ],
      taskModeOptions: [
        { value: undefined, label: '全部' },
        { value: 1, label: '上次任务开始后' },
        { value: 2, label: '上次任务完成后' }
      ],
      periodUnitOptions: [
        { value: undefined, label: '全部' },
        { value: '分', label: '分' },
        { value: '时', label: '时' },
        { value: '日', label: '日' }
      ],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '点检计划单号', key: 'code' },
          { type: 'br' },
          { type: 'br' },
          { type: 'form', name: '点检设备', key: 'bookName' },
          { type: 'form', name: '点检模板', key: 'templateName' },
          { type: 'form', name: '计划开始时间', key: 'startTime' },
          { type: 'form', name: '计划结束时间', key: 'endTime' },
          { type: 'br' },
          { type: 'br' },
          // { type: 'form', name: '下次点检时间', key: 'nextTime' },
          { type: 'form', name: '任务启动方式', key: 'taskModeName' },
          { type: 'form', name: '点检周期', key: 'period' },
          { type: 'form', name: '周期单位', key: 'periodUnit' },
          { type: 'form', name: '任务提醒时间', key: 'remindV2' },
          { type: 'form', name: '合理逾期时间', key: 'timeoutV2' },
          { type: 'br' },
          // { type: 'form', name: '维保班组', key: 'baseMaintenanceName' },
          { type: 'form', name: '点检人员', key: 'checkPersonName' },
          { type: 'form', name: '班次', key: 'shiftLabel' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          { type: 'form', name: '计划状态', key: 'statusName' }
        ]
      }
    }
  },
  computed: {
    // 分页器
    selectedNum() {
      return this.$refs.table ? this.$refs.table.selection.length : 0
    },
    ...mapGetters([
      'notificationId', 'userName'
    ])
  },
  created() {
    this.getLists()
  },
  mounted() {

  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.showDetail(row)
          break
      }
    },
    showDetail(row) {
      getInfo(row[this.mainId]).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetailOld, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看点检计划', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    getExportData(data) {
      return list(data)
    },
    getLists() {
      getTemplate().then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.code + '/' + item.name
        })
        res.data.unshift({ value: undefined, label: '全部' })
        this.templateList = res.data
      })
      getEquipmentList1({ status: 1 }).then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.code + '/' + item.name
        })
        res.data.unshift({ value: undefined, label: '全部' })
        this.equipmentList = res.data
        console.log(res.data)
      })
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            isAdd: true
          }// props
        },
        area: ['1320px', '700px'],
        title: '新增点检计划', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId],
            isAdd: false
          }// props
        },
        area: ['1320px', '700px'],
        title: '编辑点检计划', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    },
    batchEdit() {
      if (this.selectionsAll.length == 0) {
        this.$message({
          message: '请选择要操作的数据',
          type: 'warning'
        })
      } else {
        this.$layer.iframe({// 弹框
          content: {
            content: BatchEdit, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              selectionsAll: this.selectionsAll
            }// props
          },
          area: ['440px', '400px'],
          title: '批量编辑', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>

.spacebetween{

  justify-content: space-between;

  .independent_button{
    color: #333333;
    padding: 0px 15px;
    height: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 4px;
    font-size: 14px;
    /deep/ .svg-icon{
      margin-right: 10px;
    }
  }
  .independent_button:hover{
    background-color: rgba(56,123,252,.5);
    color: #fff;
  }
}
</style>
